package com.wanmei.controller.product;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.JsonResult;
import com.wanmei.pojo.Product;
import com.wanmei.service.Impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("product")
public class AddProductController {
    @Autowired
    private ProductServiceImpl productService;


    /**
     * 添加商品视图
     * @return
     */
    @RequestMapping("addProductView")
    public String addUserView() {
        return "/product/addProduct";
    }

    /**
     * 添加商品请求。
     * @param req
     * @param product
     * @return
     * @throws WanmeiException
     */
    @RequestMapping("/addProduct.json")
    @ResponseBody
    public JsonResult addProduct(HttpServletRequest req, Product product) throws WanmeiException {
        JsonResult jsonResult = new JsonResult();

        String realPath = req.getSession().getServletContext().getRealPath("");

        int isSuccess = productService.saveProductInfo(product,realPath);
        if (isSuccess==1){
            jsonResult.setCode(1);
            jsonResult.setMsg("添加成功！");
        }else {
            jsonResult.setCode(999999);
            jsonResult.setMsg("添加失败！");
        }
        return jsonResult;
    }




}
